package com.zhang;

import com.zhang.servlet.*;
import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.Wrapper;
import org.apache.catalina.core.ApplicationContext;
import org.apache.catalina.startup.Tomcat;
import org.apache.ibatis.annotations.Update;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * tomcat容器
 */
public class Main {

    AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MybatisConfig.class);

    public Main(){
        //创建一个tomcat容器
        Tomcat tomcat = new Tomcat();
        //开启8088端口
        tomcat.setPort(8083);
        //引导Http引擎
        tomcat.getConnector();

        //创建上下文对象
        Context context = tomcat.addContext("", null);


        //往tomcat容器中注册Servlet
        Wrapper wrp = Tomcat.addServlet(context, "testServlet", new TestServlet());
        //容器启动的时候就完成初始化
        wrp.setLoadOnStartup(1);
        //设置servlet请求路径，客户端只要输入test就能访问testServlet这个Servlet对象
        wrp.addMapping("/test");


        Wrapper findAll = Tomcat.addServlet(context, "findAll", applicationContext.getBean(FindAllServlet.class));
        findAll.setLoadOnStartup(1);
        findAll.addMapping("/findAll");

        Wrapper add = Tomcat.addServlet(context, "add", applicationContext.getBean(AddServlet.class));
        add.setLoadOnStartup(1);
        add.addMapping("/add");

        Wrapper del = Tomcat.addServlet(context, "del", applicationContext.getBean(DelServlet.class));
        del.setLoadOnStartup(1);
        del.addMapping("/del");

        Wrapper toUpdate = Tomcat.addServlet(context, "toUpdate", applicationContext.getBean(ToUpdateServlet.class));
        toUpdate.setLoadOnStartup(1);
        toUpdate.addMapping("/toUpdate");

        Wrapper update = Tomcat.addServlet(context, "update", applicationContext.getBean(UpdateServlet.class));
        update.setLoadOnStartup(1);
        update.addMapping("/update");


        try {
            //启动tomcat
            tomcat.start();
        } catch (LifecycleException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new Main();
    }
}
